/*
 * xAPBSCinput.java
 *
 * xAP BSC input element for MyOpenLab
 *
 * 03/11/09 by Daniel Berenguer : first version
 */

import VisualLogic.*;
import VisualLogic.variables.*;
import tools.*;

import xAP.drvCore;
import xAP.xAPtypes.TypeOfElement;
import xAP.xAPBSCtext;
import xAP.xAPBSClevel;
import xAP.xAPBSCstate;
import xAP.xAPdialog;

import java.awt.*;
import java.util.ArrayList;


/********************************************************************************************
 * xAPBSCinput class
 *********************************************************************************************/
public class xAPBSCinput extends JVSMain implements MyOpenLabDriverOwnerIF
{
    private Image image;
    private VSObject out;
    private MyOpenLabDriverIF driver ;
    private int dType = -1;
    private VSComboBox dtPin = new VSComboBox();
    private String strDataTypes[] = {"C_BOOLEAN", "C_BYTE", "C_INTEGER", "C_DOUBLE", "C_STRING"};
    private int intDataTypes[] = {ExternalIF.C_BOOLEAN, ExternalIF.C_BYTE, ExternalIF.C_INTEGER, ExternalIF.C_DOUBLE, ExternalIF.C_STRING};
    private VSPropertyDialog xAPdialog = new VSPropertyDialog();
    private VSString xAPsourceAddr = new VSString();

   /********************************************************************************************
    * getCommand
    *
    * Receive command from xAP driver
    *
    ********************************************************************************************/
    @Override
    public void getCommand(String commando, Object value)
    {
        ProcessDriverCommand(commando, value);
    }

   /********************************************************************************************
    * xPaint
    *
    * Graphical stuff
    *
    ********************************************************************************************/
    @Override
    public void xpaint(java.awt.Graphics g)
    {
        drawImageCentred(g,image);
    }

   /********************************************************************************************
    * onDispose
    *
    * Refreshes
    *
    ********************************************************************************************/
    @Override
    public void onDispose()
    {
        if (image!=null)
        {
            image.flush();
            image=null;
        }
    }

   /********************************************************************************************
    * init
    *
    * Initialization
    *
    ********************************************************************************************/
    @Override
    public void init()
    {
        initPins(0,1,0,0);
        setSize(63, 22);

        element.jSetInnerBorderVisibility(false);
        element.jSetTopPinsVisible(false);
        element.jSetBottomPinsVisible(false);
        element.jSetLeftPinsVisible(false);
        image = element.jLoadImage(element.jGetSourcePath()+"icon.gif");

        setPin(0,ExternalIF.C_BOOLEAN,element.PIN_OUTPUT);
        element.jSetPinDescription(0,"BSC input");

        // Fill data types
        int i;
        for(i=0 ; i<strDataTypes.length ; i++)
            dtPin.addItem(strDataTypes[i]);
        dtPin.selectedIndex = 0;

        if (dType > -1)
        {
            dtPin.selectedIndex = dType;
        }

        element.jSetResizable(false);
        element.jSetCaptionVisible(true);
        element.jSetCaption("x");
        setName("xAP_BSC_input");
    }

   /********************************************************************************************
    * initInputPins
    *
    * Setup input pins
    *
    ********************************************************************************************/
    @Override
    public void initInputPins()
    {
    }

   /********************************************************************************************
    * initOutputPins
    *
    * Setup output pins
    *
    ********************************************************************************************/
    @Override
    public void initOutputPins()
    {
        out = element.jCreatePinDataType(0);
        element.setPinOutputReference(0,out);
    }

   /********************************************************************************************
    * start
    *
    * Start simulation
    *
    ********************************************************************************************/
    @Override
    public void start()
    {
        ArrayList args = new ArrayList();

        // Send xAP source address
        args.add(xAPsourceAddr.getValue());

        // Send type of input
        if (out instanceof VSBoolean)
            args.add(TypeOfElement.BSC_INPUT_STA);
        else
            args.add(TypeOfElement.BSC_INPUT_NUM);

        // Open xAP driver
        driver = element.jOpenDriver("MyOpenLab.xAP", args);
        driver.registerOwner(this);

        // Request the driver for an initial value
        RequestValue();
    }

   /********************************************************************************************
    * setPropertyEditor
    *
    * Add new controls to the Property editor window
    *
    ********************************************************************************************/
    @Override
    public void setPropertyEditor()
    {
        element.jAddPEItem("Data type", dtPin, 0,0);
        element.jAddPEItem("xAP source", xAPdialog, 0,0);
    }

   /********************************************************************************************
    * propertyChanged
    *
    * Method called whenever a property from the Property editor window changes
    *
    ********************************************************************************************/
    @Override
    public void propertyChanged(Object o)
    {
        if (o.equals(dtPin))
            setDataType();

        element.jRepaint();
    }

   /********************************************************************************************
    * openPropertyDialog
    *
    * Open xAP property dialog
    *
    ********************************************************************************************/
    @Override
    public void openPropertyDialog()
    {
        xAPdialog frm = new xAPdialog(TypeOfElement.BSC_INPUT_NUM);
        frm.setModal(true);
        frm.setVisible(true);

        if (frm.getResult())
        {
            // Set source address
            String strSourceAddr = frm.getSourceAddr();
            xAPsourceAddr.setValue(strSourceAddr);
            xAPdialog.setText(strSourceAddr);
        }

        element.jRepaint();
    }

   /********************************************************************************************
    * loadFromStream
    *
    * Load properties
    *
    ********************************************************************************************/
    @Override
    public void loadFromStream(java.io.FileInputStream fis)
    {
        try
        {
            dtPin.loadFromStream(fis);
            setDataType();
            xAPsourceAddr.loadFromStream(fis);
            setSourceAddress();
            element.jRepaint();
        } catch (Exception ex){  }
    }

   /********************************************************************************************
    * saveToStream
    *
    * Save properties
    *
    ********************************************************************************************/
    @Override
    public void saveToStream(java.io.FileOutputStream fos)
    {
        dtPin.saveToStream(fos);
        xAPsourceAddr.saveToStream(fos);
    }

   /********************************************************************************************
    * setDataType
    *
    * Set data type for the xAP element
    *
    ********************************************************************************************/
    public void setDataType()
    {
        dType = intDataTypes[dtPin.selectedIndex];
        setPin(0, dType, element.PIN_OUTPUT);
    }

   /********************************************************************************************
    * setSourceAddress
    *
    * Set xAP source address for tue current element
    *
    ********************************************************************************************/
    public void setSourceAddress()
    {
        String Source = xAPsourceAddr.getValue();
        xAPdialog.setText(Source);
    }

   /********************************************************************************************
    * ProcessDriverCommand
    *
    * Process command sent by the xAP driver
    *
    * 'comando'       String command received fromt he xAP driver
    * 'value'         New endpoint value
    *
    ********************************************************************************************/
    private void ProcessDriverCommand(String commando, Object value)
    {
        if (commando.equals(drvCore.DRVCOMM_RECV_BSCVAL))
        {
            // BSC state
            if (value.getClass() == xAPBSCstate.class)
            {
                xAPBSCstate bscState = (xAPBSCstate)value;                

                if (out instanceof VSBoolean)
                {
                    VSBoolean outX=(VSBoolean)out;
                    boolean bVal = bscState.toBool();
                    outX.setValue(bVal);
                }
                else
                {
                    VSString outX=(VSString)out;
                    String strVal = bscState.toString();
                    outX.setValue(strVal);
                }
            }
            // BSC text
            else if (value.getClass() == xAPBSCtext.class)
            {
                xAPBSCtext bscText = (xAPBSCtext)value;

                if (out instanceof VSDouble)
                {
                    VSDouble outX=(VSDouble)out;
                    double dblVal = bscText.toDouble();
                    outX.setValue(dblVal);
                }
                else if (out instanceof VSInteger)
                {
                    VSInteger outX=(VSInteger)out;
                    int intVal = bscText.toInteger();
                    outX.setValue(intVal);
                }
                else if (out instanceof VSByte)
                {
                    VSByte outX=(VSByte)out;
                    byte byteVal = bscText.toByte();
                    outX.setValue(byteVal);
                }
                else
                {
                    VSString outX = (VSString)out;
                    String strVal = bscText.toString();
                    outX.setValue(strVal);
                }
            }
            // BSC level
            else if (value.getClass() == xAPBSClevel.class)
            {
                xAPBSClevel bscLevel = (xAPBSClevel)value;

                if (out instanceof VSDouble)
                {
                    VSDouble outX=(VSDouble)out;
                    double dblVal = bscLevel.toDouble();
                    outX.setValue(dblVal);
                }
                else if (out instanceof VSInteger)
                {
                    VSInteger outX=(VSInteger)out;
                    int intVal = bscLevel.toInteger();
                    outX.setValue(intVal);
                }
                else if (out instanceof VSByte)
                {
                    VSByte outX=(VSByte)out;
                    byte byteVal = bscLevel.toByte();
                    outX.setValue(byteVal);
                }
                else
                {
                    VSString outX = (VSString)out;
                    String strVal = bscLevel.toString();
                    outX.setValue(strVal);
                }
            }

            out.setChanged(true);
            element.notifyPin(0);
        }
    }

   /********************************************************************************************
    * RequestValue
    *
    * Request current value
    *
    ********************************************************************************************/
    private void RequestValue()
    {
        driver.sendCommand(drvCore.DRVCOMM_REQU_BSCVAL, xAPsourceAddr);
    }
}
